package com.gitee.sys.impl;


import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gitee.mybase.top.entity.SysResource;
import com.gitee.sys.mapper.SysResourceMapper;
import com.gitee.sys.pojo.Menu;
import com.gitee.sys.pojo.MenuBar;
import com.gitee.sys.pojo.MenuItem;
import com.gitee.sys.service.ISysResourceService;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author chengkaimao
 * @since 2018-11-17
 */
@Service
@Component
public class SysResourceServiceImpl extends ServiceImpl<SysResourceMapper, SysResource> implements ISysResourceService {

    @Override
    public List<MenuBar> getMenuBar(String appCode) {
        return baseMapper.getMenuBar(appCode, "mck");
    }

    @Override
    public List<Menu> getMenu(String menuBarCode) {
        return baseMapper.getMenu(menuBarCode, "mck");
    }

    @Override
    public List<MenuItem> getMenuItem(String menuCode) {
        return baseMapper.getMenuItem(menuCode, "mck");
    }

    @Override
    public List<MenuItem> getMenuItemByBar(String menuBarCode) {
        return baseMapper.getMenuItemByBar(menuBarCode, "mck");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int saveResource(SysResource sysResource) {
        sysResource.setCreateTime(LocalDateTime.now());
        sysResource.setIsLock(0);
        sysResource.setIsValid(1);
        return baseMapper.insert(sysResource);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateResource(SysResource sysResource) {
        UpdateWrapper<SysResource> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("res_name", sysResource.getResName());
        updateWrapper.set("type", sysResource.getType());
        updateWrapper.set("path", sysResource.getType());
        updateWrapper.set("model", sysResource.getModels());
        updateWrapper.set("icon", sysResource.getIcon());
        updateWrapper.set("parent_code", sysResource.getParentCode());
        updateWrapper.eq("res_code", sysResource.getResCode());
        return baseMapper.update(sysResource, updateWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int delResource(String resourceCode) {
        QueryWrapper<SysResource> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("res_code", resourceCode);
        return baseMapper.delete(queryWrapper);
    }
}
